package com.xxg.renrensearch.service.common;

import com.xxg.renrensearch.domain.result.ExtraInfo;
import com.xxg.renrensearch.domain.result.ShopCarryTypeCount;
import com.xxg.renrensearch.domain.result.ShopWithProducts;
import com.xxg.renrensearch.service.StatisticsService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公共的方法
 *
 * @author xuxiaogang
 * @date 2021-12-07
 */
@Service
public class CommonService {
    private final StatisticsService statisticsService;

    public CommonService(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    /**
     * 处理店铺上的额外字段信息
     *
     * @param shopId  店铺ID
     * @param content 结果集合
     */
    public void extraInfoProcess(Integer shopId, List<ShopWithProducts> content) {
        // 获取店铺spu数量
        Map<Integer, Integer> shopSpuCount = statisticsService.getShopSpuCount();
        // 获取店铺不同权限、不同类型的数量
        Map<Integer, ShopCarryTypeCount[]> caryTypeCountMap = statisticsService.getShopCarryTypeSpuCount();
        // 获取店铺采集数量
        Map<Integer, Map<Integer, Integer>> gatherMap = statisticsService.getShopGatherCount();
        // 获取店铺采集数量
        Map<Integer, Map<Integer, Integer>> carryMap = statisticsService.getShopCarriedCount();
        Map<Integer, Integer> gather = gatherMap.getOrDefault(shopId, new HashMap<>(0));
        Map<Integer, Integer> carry = carryMap.getOrDefault(shopId, new HashMap<>(0));
        content.forEach(e -> {
            ExtraInfo extraInfo = new ExtraInfo();
            e.setExtraInfo(extraInfo);
            Integer id = e.getShop().getId();
            // 判断是否为自己的店铺
            if (id.equals(shopId)) {
                e.setMyShop(true);
            }
            // 获取商店可带货数量
            Integer spuCount = shopSpuCount.getOrDefault(id, 0);
            if (spuCount > 0) {
                ShopCarryTypeCount[] caryTypeCounts = caryTypeCountMap.get(id);
                if (caryTypeCounts != null) {
                    // 获取全局设置
                    ShopCarryTypeCount count0 = caryTypeCounts[0];
                    if (count0 != null) {
                        // 全局允许设置
                        if (count0.getCarryAble()) {
                            // 统计不允许带货数量
                            ShopCarryTypeCount count2 = caryTypeCounts[2];
                            int noCarryCount = 0;
                            if (count2 != null) {
                                noCarryCount = count2.getCount();
                            }
                            extraInfo.setCarryAbleCount(spuCount - noCarryCount);
                        } else {
                            // 统计允许带货数量
                            ShopCarryTypeCount count1 = caryTypeCounts[1];
                            int carryCount = 0;
                            if (count1 != null) {
                                carryCount = count1.getCount();
                            }
                            extraInfo.setCarryAbleCount(carryCount);
                        }
                    } else {
                        // 默认全局允许带货，则统计不允许带货数量
                        ShopCarryTypeCount count2 = caryTypeCounts[2];
                        int noCarryCount = 0;
                        if (count2 != null) {
                            noCarryCount = count2.getCount();
                        }
                        extraInfo.setCarryAbleCount(spuCount - noCarryCount);
                    }
                }else{
                    extraInfo.setCarryAbleCount(spuCount);
                }
            }
            // 设置已带货数量
            extraInfo.setCarriedCount(carry.getOrDefault(id, 0));
            // 设置已采集数量
            extraInfo.setGatheredCount(gather.getOrDefault(id, 0));
            // 已带货数大于0，表示合作关系
            extraInfo.setRelated(extraInfo.getCarriedCount() > 0);
        });
    }
}
